<?php

namespace App\Admin\Controllers;

use App\Models\GoldRound;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\Models\GoldRecord;
use Illuminate\Support\Str;
use App\Admin\Actions\GoldRecordAction\ExamineRecord;
use App\Admin\Actions\GoldRecordAction\CloseRecord;
use App\Admin\Actions\GoldRecordAction\downRecord;
use Encore\Admin\Facades\Admin;

class GoldRecordsController extends AdminController
{
    protected $title = '金店抢购记录';

     protected function getdown(Grid $grid)
    {
	    if ($upGoldRecordId = request('up_gold_record_id')) {
		    $grid->model()->where('up_gold_record_id', $upGoldRecordId)->orderByDesc('id');
	    }
	  
    }


    protected function grid()
    {

    	$roleId = Admin::user()->isAdministrator();
    	$grid = new Grid(new GoldRecord());
    	if($roleId){

			$grid->model()->orderByDesc('id');
    	}else{

    		$grid->model()->join('users','users.id','gold_records.user_id')->select("gold_records.*")->where("users.admin_user_id",Admin::user()->id)->orderByDesc('gold_records.id');
    	}
      


        
         $this->getdown($grid);

        $grid->column('id', 'ID');

	    $grid->column('user_id', '会员')->display(function () {
		    return $this->user ? $this->user->nameText(true) : '';
	    });

	    $grid->column('gold_seed_id', '种子')->display(function () {
		    return Str::limit($this->goldSeed->title, 20);
	    });
	    $grid->column('gold_round_id', '场次')->display(function () {
		    return $this->goldRound->title;
	    });
        $grid->column('gold_product_id', '抢购商品')->display(function ($value) {
            return "<small>商品</small> {$value} (<small>轮</small> {$this->round})";
        });
        $grid->column('product_id', '原始商品')->display(function ($value) {
        	return $value ? Str::limit($this->product->title, 20) : '';
        });
        $grid->column('record_all_price', '记录总价');
       /* $grid->column('pay_net_price', '权益金额');
        $grid->column('pay_cash_price', '抢购金');*/
        $grid->column('pay_gold_beans', '金豆消耗');
        $grid->column('resell_all_price', '寄卖总价');

        $grid->column('up_gold_record_id', '寄售人')->display(function () {
        	if($this->up_gold_record_id){

        		return isset($this->upGoldRecord->user->name) ? $this->upGoldRecord->user->id." - ".$this->upGoldRecord->user->name : '';

        	}
        	 return '';
        });
        $grid->column('paid_at', '付款')->display(function ($value) {
        	return $value ? _true() : '';
        });
        $grid->column('closed_at', '关闭')->display(function ($value) {
            return $value ? _false() : '';
        });

          $grid->column('is_del', '重复单删除')->display(function ($value) {
            return $value ? _true() : '';
        });



         $grid->column('is_contact', '联系客服')->display(function ($value) {
         	if($this->goldRecordVoucher){
         		return $this->goldRecordVoucher->is_contact  ? _true() : '';
         	}else{

         		return $this->is_contact  ? _true() : '';
         	}
        });
        
        $grid->column('to_resell_applied_at', '寄售时间')->display(function ($value) {
	        return $value ? $this->to_resell_applied_at->toDatetimeString() : '';
        });
        $grid->column('to_split_applied_at', '拆单时间')->display(function ($value) {
	        return $value ? $this->to_split_applied_at->toDatetimeString() : '';
        });
        $grid->column('to_take_applied_at', '提货时间')->display(function ($value) {
	        return $value ? $this->to_take_applied_at->toDatetimeString() : '';
        });
        $grid->column('finished_at', '完成时间')->display(function ($value) {
	        return $value ? $this->finished_at->toDatetimeString() : '';
        });
      /*  $grid->column('settled_at', '向上结算')->display(function ($value) {
        	return $value ? $this->settled_at->toDatetimeString() : '';
        });*/
        $grid->column('rewarded_at', '奖金结算')->display(function ($value) {
        	return $value ? $this->rewarded_at->toDatetimeString() : '';
        });
	    $grid->column('created_at', '创建')->display(function () {
		    return $this->created_at->toDatetimeString();
	    });
	    $grid->column('voucher', '凭证')->display(function ($value) {
         	if($this->goldRecordVoucher){
         		return $this->goldRecordVoucher->voucher  ? $this->goldRecordVoucher->voucher : '';
         	}
            return '';
        })->lightbox(['width' => 50, 'height' => 50]);
	    $grid->disableRowSelector();
	    $grid->disableColumnSelector();
	    $grid->disableExport();
	    $grid->disableCreateButton();
	    //$grid->disableActions();
	    $this->gridFilter($grid);
	    $this->gridActions($grid);

        return $grid;
    }


    protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableDelete();
			$actions->disableEdit();
			$actions->add(new downRecord());
			$actions->add(new ExamineRecord());
			$actions->add(new CloseRecord());
		});
	}

	protected function gridFilter(Grid $grid)
	{
		$grid->filter(function (Grid\Filter $filter) {
			$filter->expand();

			$filter->column(1/2, function (Grid\Filter $filter) {
				$filter->disableIdFilter();
				$filter->equal('user_id', '会员');
				$filter->equal('id', '记录');
				$filter->equal('gold_product_id', '商品');
				$filter->equal('cache_id', '记录单号');
				$filter->equal('gold_round_id', '场次')->select(GoldRound::all()->pluck('title', 'id'));
			});

			$filter->column(1/2, function (Grid\Filter $filter) {

			});

			$filter->scope('trashed', '回收站')->onlyTrashed();
		});
	}
}
